package tests;

import common.Utils;
import org.openqa.selenium.By;

import java.io.IOException;
import java.time.Duration;

public class LoginPage extends Utils {

    public static String url = "http://127.0.0.1:9580/sign-in.html";
    public static String res = "";

    public LoginPage() {
        super(url);
    }

    //检查页面是否加载成功
    public  void loginPageRight() throws InterruptedException, IOException {
        //通过查看页面元素是否存在来检查页面加载成功与否
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2"));
        //登陆输入框
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div"));
    }

    //检查输入框是否可以正常输入、是否可以点击显示密码按钮明文显示密码与第二次点击是否可以加密
    public void BasicTest() throws InterruptedException, IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("111");
        String edit = driver.findElement(By.cssSelector("#username")).getAttribute("value");
        assert edit.equals("111");

        driver.findElement(By.cssSelector("#password")).sendKeys("11122");
        edit = driver.findElement(By.cssSelector("#password")).getAttribute("value");
        assert edit.equals("11122");

        driver.findElement(By.cssSelector("#password_a")).click();
        res = driver.findElement(By.cssSelector("#password")).getAttribute("type");
        assert res.equals("text");

        driver.findElement(By.cssSelector("#password_a")).click();
        res = driver.findElement(By.cssSelector("#password")).getAttribute("type");
        assert res.equals("password");
    }

    //检查登录功能---成功登录
    public void LoginSuc() throws InterruptedException, IOException {
        //        //方法一：通过clear保证输入框没有文本
//        driver.findElement(By.cssSelector("#username")).clear();
//        driver.findElement(By.cssSelector("#password")).clear();
        //方法二：通过refresh保证输入框没有文本
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("Morningstar01");
        driver.findElement(By.cssSelector("#password")).sendKeys("112233");
        driver.findElement(By.cssSelector("#submit")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        //检查登陆页面是否登陆成功
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > h1 > a > img"));
        //页面标题来检查是否登录成功
        String expect = driver.getTitle();
        assert expect.equals("比特论坛");

        //检查第二个账号登录
        driver.navigate().back();
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("Morningstar");
        driver.findElement(By.cssSelector("#password")).sendKeys("112233");
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(2000);
        //检查登陆页面是否登陆成功
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > h1 > a > img"));
        //页面标题来检查是否登录成功
        expect = driver.getTitle();
        assert expect.equals("比特论坛");
    }

    //检查登录动能--登录失败
    public void LoginFail() throws InterruptedException, IOException {
//        //方法一：通过clear保证输入框没有文本
//        driver.findElement(By.cssSelector("#username")).clear();
//        driver.findElement(By.cssSelector("#password")).clear();
        //方法二：通过refresh保证输入框没有文本
        driver.navigate().back();
        driver.navigate().refresh();

        //用户名或密码错误
        driver.findElement(By.cssSelector("#username")).sendKeys("Morningstar");
        driver.findElement(By.cssSelector("#password")).sendKeys("111");
        driver.findElement(By.cssSelector("#submit")).click();

        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        res = res.substring(res.lastIndexOf("\n") + 1).trim(); // 找到最后一个换行符，截取后面的内容
        assert res.equals("用户名或密码错误");

        //不输入用户名/密码能否登录
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("Morningstar");
        driver.findElement(By.cssSelector("#submit")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        res = driver.findElement(By.cssSelector("#signInForm > div.mb-2 > div > div")).getText();
        assert res.equals("密码不能为空");

        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#password")).sendKeys("111");
        driver.findElement(By.cssSelector("#submit")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        res = driver.findElement(By.cssSelector("#signInForm > div.mb-3 > div")).getText();
        assert res.equals("用户名不能为空");
    }

    //点击点击注册按钮进入注册页面
    public void EnterRegistPage() throws IOException {
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a")).click();
        res = driver.findElement(By.cssSelector("#signUpForm > div > h2")).getText();
        getScreenShot(getClass().getName());
        assert res.equals("用户注册");
    }
}
